package com.lry.jvm.instructions.stores;

import com.lry.jvm.instructions.base.NoOperandsInstruction;
import com.lry.jvm.rtda.Frame;
import com.lry.jvm.rtda.JObject;
import com.lry.jvm.rtda.OperandStack;
import com.lry.jvm.util.Util;

public class castore extends NoOperandsInstruction {
    @Override
    public byte getOpCode() {
        return 0x55;
    }

    @Override
    public void execute(Frame frame) throws Exception {
        OperandStack stack = frame.getOperandStack();
        //
        char c = (char) stack.popInt();

        int index = stack.popInt();
        JObject arrRef = stack.popRef();
        if(null==arrRef){
            Util.panic("java.lang.NullPointerException");
        }

        char[] refs = arrRef.toChars();
        if(index<0||index>=refs.length){
            Util.panic("ArrayIndexOutOfBoundsException");
        }
        refs[index] = c;
    }
}